package com.etv.launcher.android.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.BatteryManager;

import com.etv.launcher.common.utils.ToastUtil;
import com.etv.launcher.repository.ApplicationInfoRepository;

import java.util.Objects;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class GlobalBroadcastReceiver extends BroadcastReceiver {

    @Inject
    ApplicationInfoRepository applicationInfoRepository;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action != null) {
            switch (action) {

                //电量改变
                case Intent.ACTION_BATTERY_CHANGED:
                    int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                    int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
                    float batteryLevel = (level * 100) / (float) scale;
                    ToastUtil.show(context,"电量："+batteryLevel);
                    applicationInfoRepository.setBatteryLevel(batteryLevel);
                    break;

                //新增应用
                case Intent.ACTION_PACKAGE_ADDED:
                    applicationInfoRepository.addApplication(Objects.requireNonNull(intent.getData()).getSchemeSpecificPart());
                    break;

                //卸载应用
                case Intent.ACTION_PACKAGE_REMOVED:
                    applicationInfoRepository.removeApplication(Objects.requireNonNull(intent.getData()).getSchemeSpecificPart());
                    ToastUtil.show(context,"卸载成功！");
                    break;

                //网络变化
                case ConnectivityManager.CONNECTIVITY_ACTION:
                    ToastUtil.show(context, "监听到网络变化");
                    break;

                //电源连接
                case Intent.ACTION_POWER_CONNECTED:
                    ToastUtil.show(context, "连接电源");
                    break;

                //电源断连
                case Intent.ACTION_POWER_DISCONNECTED:
                    ToastUtil.show(context, "拔掉电源");
                    break;
            }
        }
    }
}
